package com.lej.app_main.provider

import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.lej.app_main.mainService
import com.lej.core.common.*
import com.lej.core.provider.UnityProvider
import com.lej.core.router.CRouter
import com.lej.core.utils.extention.router
import com.lej.core.utils.extention.toast
import com.lej.dataservice.club.bean.main.unity.UnityMiniGameFrom
import com.lej.dataservice.club.http.checkData
import com.lej.dataservice.club.http.handleMsg
import kotlinx.coroutines.launch

object UnityProviderImpl : UnityProvider {
    override suspend fun enter3DRoom(act: AppCompatActivity, houseId: Int, showDialog: Boolean) {
        if (!initUserInfo()) return

        linkToRoom(act, houseId)

//        if (PCacheInfo.isAuth) {
//            linkToRoom(act, houseId)
//        } else {
//            if (showDialog) {
//                RealNameAuthDialog.show(act)
//            } else {
//                "需要实名认证方能进入空间".toast()
//            }
//        }
    }

    override fun openShop(
        act: AppCompatActivity,
        shopType: String?,
        shopId: Int,
        goodsId: Int,
        isAvatar: Boolean
    ) {
        act.lifecycleScope.launch {
            if (!initUserInfo()) return@launch
            act.router(CRouter.unityhouse) {
                withInt(UnityRoomId, 1)
                withInt(UnityRoomType, UnityMiniGameFrom.ENTER_SHOP_PAGE)
                withString(UnityRoomShopType, shopType)
                withInt(UnityRoomShopId, shopId)
                withInt(UnityRoomGoodsId, goodsId)
                withBoolean(UnityRoomIsAvatar, isAvatar)
            }
        }
    }

    override fun openChat(act: AppCompatActivity) {
        act.open2D(UnityMiniGameFrom.CHAT_PAGE)
    }

    override fun openSearchView(act: AppCompatActivity) {
        act.open2D(UnityMiniGameFrom.SEARCH_PAGE)
    }

    override fun openCard(act: AppCompatActivity, targetUid: Int) {
        act.open2D(UnityMiniGameFrom.VISITING_CARD_PAGE, targetUid)
    }

    private fun AppCompatActivity.open2D(type: Int, targetUid: Int? = null) {
        lifecycleScope.launch {
            if (!initUserInfo()) return@launch
            router(CRouter.unityhouse) {
                withInt(UnityRoomType, type)
                targetUid?.let { withInt(UnityTargetUid, it) }
            }
        }
    }

    private suspend fun linkToRoom(act: AppCompatActivity, houseId: Int) {
        //attr 1 可链接 2 服务排队已满不可链接
        val attr = runCatching { mainService.tcpAttr(houseId).checkData().get("attr").asInt }
            .onFailure { it.handleMsg.toast();return }.getOrNull()
        if (attr != 1) {
            "房间已满,请稍后重试".toast()
            return
        }
        act.router(CRouter.unityhouse) {
            withInt(UnityRoomId, houseId)
            withInt(UnityRoomType, UnityMiniGameFrom.ENTER_HOUSE)
        }
    }

    private suspend fun initUserInfo(): Boolean {
        if (PCacheInfo.imId.isNotEmpty()) return true
        runCatching {
            val info = mainService.getUserInfo().checkData()
            PCacheInfo.imId = info.tencent_id ?: ""
            PCacheInfo.imToken = info.tencent_user_sig ?: ""
            PCacheInfo.userId = info.id
            PCacheInfo.isAuth = info.isAuthentication()
        }
        if (PCacheInfo.imId.isEmpty()) {
            "用户信息异常,请稍后重试".toast()
        }
        return PCacheInfo.imId.isNotEmpty()
    }
}